import { GameMode } from "@minecraft/server";
import { WereWolfModule } from "@/werewolf/module";
import { WereWolfStatus } from "@/werewolf/player";

export class WereWolfPlayerStateUpdater extends WereWolfModule.Component {
    override onAttach(): void {
        this.updatePlayerStatus();
    }

    updatePlayerStatus() {
        this.context.players.getAll().forEach((p) => {
            if (p.status == WereWolfStatus.justDead) {
                p.status = WereWolfStatus.Dead;
            }
            if (p.status == WereWolfStatus.killed) {
                p.status = WereWolfStatus.justDead;
                p.sendMessage("没人救你，你死了");
            }
            //改模式
            if (p.status >= WereWolfStatus.justDead) {
                p.player?.setGameMode(GameMode.Spectator);
            } else {
                p.player?.setGameMode(
                    this.context.config.restrictMode
                        ? GameMode.Adventure
                        : GameMode.Survival
                );
            }
        });
    }
}
